<template>
    <div class="topimg">季度养殖成本分析</div>
  <div class="aqua-cost-table">
    <table v-if="costs.length">
      <!-- <caption>
        季度养殖成本分析
      </caption> -->
      <thead>
        <tr>
          <th>费用类型</th>
          <th v-for="month in months" :key="month">{{ month }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(cost, index) in costs" :key="index">
          <td class="cost-type">{{ cost.name }}</td>
          <td v-for="(value, i) in cost.values" :key="i">{{ value }}</td>
        </tr>
      </tbody>
    </table>
    <div v-else style="color: white; text-align: center; padding: 20px">
      暂无数据
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import { monitorApi } from "@/api";

export default {
  name: "AquaCostTable",
  setup() {
    const months = ref(["四月份", "七月份", "九月份", "十二月份"]);
    const costs = ref([]);
    onMounted(async () => {
      const res = await monitorApi.calculateBenefit({});
      // 表的月份是假数据，另外，下面两行没有数据(水电杂费\药物防疫)是因为后端没有发
      if (res.code === 0 && res.data) {
        costs.value = [
          {
            name: "鱼苗费用",
            values: res.data.map((item) => item.seedlingCost) || [],
          },
          {
            name: "饲料费用",
            values: res.data.map((item) => item.feedCost) || [],
          },
          {
            name: "人工费用",
            values: res.data.map((item) => item.laborCost) || [],
          },
        ];
      }
    });
    return { months, costs };
  },
};
</script>

<style scoped>
.aqua-cost-table {
  width: 100%;
  height: 360px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: space-between; /* 改为垂直分布 */
  padding: 20px; /* 添加内边距避免内容贴边 */
  box-sizing: border-box;
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 10px;
}

table {
  width: 100%;
  height: 100%; /* 占据容器全部高度 */
  border-collapse: collapse;
  table-layout: fixed;
  flex-grow: 1; /* 允许表格扩展 */
}

caption {
  font-size: 18px;
  font-weight: bold;
  padding: 10px;
  color: white;
  background-color: #0a1a3a;
}

th,
td {
  padding: 6px 8px;
  text-align: center;
  color: white;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 14px;
  word-wrap: break-word;
}

th {
  font-weight: 500;
  background-color: rgba(0, 0, 0, 0.2);
}

.cost-type {
  text-align: left;
  padding-left: 12px;
}

tr:hover {
  background-color: rgba(255, 255, 255, 0.05);
}

/* 防止小屏幕撑破高度 */
@media (max-width: 768px) {
  caption {
    font-size: 16px;
    padding: 6px;
  }

  th,
  td {
    font-size: 12px;
    padding: 4px 6px;
  }
}

tbody {
  height: 100%; /* 让表格主体占满 */
}

tr {
  height: auto; /* 自动高度 */
  flex-grow: 1; /* 允许每行扩展 */
}

.topimg{
  font-size: 24px;
  height: 40px;
  background-image: url(@/assets/top.png);
  background-size: cover;
  text-align: center;
}
</style>
